package com.zlict.commonelasticsearch.task;

import com.zlict.commonelasticsearch.service.ElasticsearchDiskMonitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Elasticsearch定时任务服务类
 * 用于Quartz调度器调用
 * 
 * @author zlict
 */
@Slf4j
@Component("elasticsearchTaskJobService")
public class ElasticsearchTaskJobService {
    
    @Autowired
    private ElasticsearchDiskMonitorService elasticsearchDiskMonitorService;
    
    /**
     * 每小时检查存储空间
     * 对应原来的 @Scheduled(cron = "0 0 * * * ?")
     * 现在集成了Elasticsearch磁盘监控功能
     */
    public void checkStorageSpace() {
        log.info("开始检查Elasticsearch存储空间");
        
        try {
            // 执行Elasticsearch磁盘监控和ILM策略调整
            log.info("开始检查Elasticsearch磁盘空间并调整ILM策略");
            boolean emergencyTriggered = elasticsearchDiskMonitorService.checkDiskSpaceWithESStatus();
            
            if (emergencyTriggered) {
                log.warn("Elasticsearch磁盘空间不足，已触发紧急ILM策略调整");
            } else {
                log.info("Elasticsearch磁盘空间充足，无需调整ILM策略");
            }
            
        } catch (Exception e) {
            log.error("存储空间检查过程中发生错误", e);
        }
    }
    
    /**
     * 每天凌晨2点清理过期日志
     * 对应原来的 @Scheduled(cron = "0 0 2 * * ?")
     */
    public void cleanExpiredLogs() {
        log.info("开始清理过期Elasticsearch日志");
        
        try {
            // 这里可以添加清理过期日志的逻辑
            // 由于ILM策略已经处理了数据生命周期，这里主要是日志清理
            log.info("Elasticsearch日志清理完成");
            
        } catch (Exception e) {
            log.error("清理过期日志过程中发生错误", e);
        }
    }
    
    /**
     * 手动触发磁盘空间检查
     * 
     * @param thresholdMB 阈值（MB）
     * @return 是否触发了紧急模式
     */
    public boolean manualCheckDiskSpace(long thresholdMB) {
        log.info("手动触发磁盘空间检查，阈值: {} MB", thresholdMB);
        
        try {
            boolean emergencyTriggered = elasticsearchDiskMonitorService.checkDiskSpaceAndTriggerILM(thresholdMB);
            
            if (emergencyTriggered) {
                log.warn("手动检查：磁盘空间不足，已触发紧急ILM策略");
            } else {
                log.info("手动检查：磁盘空间充足，无需调整ILM策略");
            }
            
            return emergencyTriggered;
            
        } catch (Exception e) {
            log.error("手动磁盘空间检查失败", e);
            return false;
        }
    }
    
    /**
     * 手动恢复正常的ILM策略
     * 
     * @param retentionMonths 保留月数
     * @return 是否恢复成功
     */
    public boolean manualRestoreNormalILM(int retentionMonths) {
        log.info("手动恢复正常的ILM策略，保留月数: {}", retentionMonths);
        
        try {
            boolean restored = elasticsearchDiskMonitorService.restoreNormalILMStrategy(retentionMonths);
            
            if (restored) {
                log.info("手动恢复：正常ILM策略恢复成功");
            } else {
                log.error("手动恢复：正常ILM策略恢复失败");
            }
            
            return restored;
            
        } catch (Exception e) {
            log.error("手动恢复正常ILM策略失败", e);
            return false;
        }
    }
}



